/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.LinkedBlockingQueue;

/**
 *
 * @author zwlin
 */
public class MD5Pool {

	private static MD5Pool ins = null;

	public static MD5Pool getInstance() {
		if (ins == null) {
			synchronized (MD5Pool.class) {
				if (ins == null) {
					ins = new MD5Pool();
				}
			}
		}
		return ins;
	}

	private MD5Pool() {
		pool = new LinkedBlockingQueue<>();
	}

	private LinkedBlockingQueue<MessageDigest> pool;

	public MessageDigest Borrow() throws NoSuchAlgorithmException {
		try {
			return pool.remove();
		} catch (Exception e) {
			return MessageDigest.getInstance("MD5");
		}
	}

	public void Return(MessageDigest md) {
		pool.add(md);
	}
}
